import {useReducer} from 'react'

const USERNAME = 'USERNAME'
const PASSWORD = 'PASSWORD'
const PASSWORDAGAIN = 'PASSWORDAGAIN'
const EMAIL = 'EMAIL'
const PHONE = 'PHONE'
const CODE = 'CODE'

const createAction = (type, value) => ({type, value})

const reducer = (state, action) => {
  switch (action.type){
    case USERNAME:
      return {...state, username: action.value}
    case PASSWORDAGAIN:
      return {...state, passwordAgain: action.value}
    case PASSWORD:
      return {...state, password: action.value}
    case EMAIL:
      return {...state, email: action.value}
    case PHONE:
      return {...state, phone: action.value}
    case CODE:
      return {...state, code: action.value}
    default:
      return state
  }
}
const initialState = {
  username: '',
  password: '',
  passwordAgain: '',
  email: '',
  phone: '',
  code: '',
}
function useRegisterState() {
  const [regState, regStateDispatch] = useReducer(reducer, initialState)

  return {regState, regStateDispatch}
}

export {useRegisterState, createAction}